//
//  Action_Command.m
//  ActiveDeck
//
//  Created by Michael Cohen on 12/21/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Action_Command.h"
#import "GDataXMLNode.h"
#import "HttpRequest.h"

@implementation Action_Command
@synthesize method, data, endpoint, message, PID;
-(void)execute:(id)pvc{
    GDataXMLNode *defaultNamespace = [GDataXMLNode attributeWithName:@"xmlns" 
                                                         stringValue:@"http://www.activedeck.com/PowerPoint/"];
    
    GDataXMLElement *rootElement = [[GDataXMLElement alloc] initWithXMLString:@"<ActiveUpdate></ActiveUpdate>" error:nil];
    [rootElement addChild:defaultNamespace];
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *ACTIVEDECKKIOSK = [prefs stringForKey:@"Kiosk"];
    
    GDataXMLElement *messageElement = [[GDataXMLElement alloc] initWithXMLString:[NSString stringWithFormat:@"<Message Channel=\"%d_K_%@\"></Message>", PID, ACTIVEDECKKIOSK] error:nil];
    [messageElement addChild:[[GDataXMLElement alloc] initWithXMLString:data error:nil]];
    
    [rootElement addChild:messageElement];
    
    
    GDataXMLDocument *document = [[[GDataXMLDocument alloc] initWithRootElement:rootElement] autorelease];
    
    NSData *xData = document.XMLData;
    
    NSString *commandData = [[NSString alloc] initWithData:xData encoding:NSUTF8StringEncoding];
    
    HttpRequest *commandRequest = [[HttpRequest alloc] init];
    
    NSString *result = [commandRequest XML_PostRequest:commandData urlString:[NSString stringWithFormat:@"%@/%@/%d", endpoint, method, PID]];
    
    if([result isEqualToString:@"Error"]){
        message = [message initWithTitle:@"Error!" 
                                 message:@"There was a problem publishing" 
                                delegate:self 
                       cancelButtonTitle:@"Please seek assistance." 
                       otherButtonTitles:nil];
        
        [message show];
        [message release];
    }
}
@end
